#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <mcbeealgorithm.h>
#include "mcavgbee.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),gameWidget(0),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    algorithms << new SolverBeeAlgorithm();
    algorithms << new MCbeeAlgorithm();
    algorithms << new mcAvgBee();
    foreach (SGalgorithm *algorithm, algorithms) {
         ui->comboBox_method->addItem(QString::fromStdString(*algorithm));
    }
        ui->spinBox_EP->setValue(SolverBeeAlgorithm::eBees);
        ui->spinBox_NP->setValue(SolverBeeAlgorithm::bBees);
        ui->spinBox_EZ->setValue(SolverBeeAlgorithm::eSources);
        ui->spinBox_NZ->setValue(SolverBeeAlgorithm::bSources);
        ui->spinBox_LP->setValue(SolverBeeAlgorithm::bees);
        ui->spinBox_I->setValue(SolverBeeAlgorithm::iterations);

        ui->groupBox_wyniki->hide();
        this->updateGeometry();


}
MainWindow::~MainWindow()
{
    foreach (SGalgorithm *algorithm, algorithms) {
        delete algorithm;
    }
    delete ui;
}

void MainWindow::on_pushButton_new_clicked()
{
    if(gameWidget!=0)
    {
        delete gameWidget;
    }
    gameWidget =  new GLWidget(0,ui->spinBox_W->value(),ui->spinBox_H->value(),ui->spinBox_C->value());
    gameWidget->resize(600,400);
    gameWidget->show();
    ui->groupBox_wyniki->hide();
    this->updateGeometry();
}

void MainWindow::on_pushButton_save_clicked()
{
    QString fName  = QFileDialog::getSaveFileName(this,"Save game");
    //if(gameWidget) gameWidget->s->save(fName.toStdString());
}

void MainWindow::on_pushButton_Load_clicked()
{

}

void MainWindow::on_pushButton_Solve_clicked()
{
    if(gameWidget==0) return;
    int index = ui->comboBox_method->currentIndex();
    if(index > algorithms.size())
    {

    }
    else
    { 
        solution = algorithms[index]->solve(*gameWidget->s);
        sameGame *s = new sameGame(*gameWidget->s);

        if(gameWidget!=0)
        {
            gameWidget->hide();
           // gameWidget->deleteLater();
          delete gameWidget;
        }
        gameWidget =  new GLWidget(0,s,solution);
        gameWidget->resize(gameWidget->s->w()*30,gameWidget->s->h()*30);
        gameWidget->show();

        ui->label_LiczbaKrokow->setText(QString::number(solution.steps.size()));
        ui->label_LiczbaPonktow->setText(QString::number(solution.score));
        ui->label_PozostaleBloki->setText(QString::number(solution.BlocksLefted));
        ui->groupBox_wyniki->show();
        this->updateGeometry();
    }

}

void MainWindow::on_comboBox_method_activated(int index)
{

}

void MainWindow::on_comboBox_method_currentIndexChanged(int index)
{

}

void MainWindow::on_pushButton_start_predef_clicked()
{
    if(gameWidget!=0)
    {
        delete gameWidget;
    }
    gameWidget =  new GLWidget(0,ui->spinBox_W->value(),ui->spinBox_H->value(),ui->spinBox_C->value(),ui->comboBox_predef->currentIndex());
    gameWidget->resize(550,600);
    gameWidget->show();
}

void MainWindow::on_spinBox_LP_valueChanged(int arg1)
{
    SolverBeeAlgorithm::bees = arg1;

}

void MainWindow::on_spinBox_EP_valueChanged(int arg1)
{
    SolverBeeAlgorithm::eBees = arg1;
}

void MainWindow::on_spinBox_NP_valueChanged(int arg1)
{
    SolverBeeAlgorithm::bBees = arg1;
}

void MainWindow::on_spinBox_EZ_valueChanged(int arg1)
{
    SolverBeeAlgorithm::eSources = arg1;
}

void MainWindow::on_spinBox_NZ_valueChanged(int arg1)
{
    SolverBeeAlgorithm::bSources = arg1;
}

void MainWindow::on_spinBox_I_valueChanged(int arg1)
{
    SolverBeeAlgorithm::iterations = arg1;
}
